//
//  WBNavigationController.swift
//  WJ微博
//
//  Created by 王杰 on 2017/1/16.
//  Copyright © 2017年 WangJie. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 隐藏默认的 navigationBar
        navigationBar.isHidden = true
    }

    // 重写父类方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        print(viewController)
        
        // 如果不是栈底控制器才需要隐藏
        if childViewControllers.count > 0 { // 说明此时不是栈底控制器
            // 隐藏底部tabBar
            hidesBottomBarWhenPushed = true
            
            // 判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                
                // 判断控制器的级数，只有一个子控制器的时候，显示栈底控制器的标题
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                // 取出自定义的navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack:true )
            }
        }

        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func popToParent() {
        
        popViewController(animated: true)
    }
}
